package com.ruoyi.energy.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.energy.domain.NyTransPart;
import com.ruoyi.energy.domain.vo.NyTransPartExcelVO;
import com.ruoyi.energy.service.INyTransPartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 变压器-部件Controller
 *
 * @author cjie
 * @date 2021-10-11
 */
@Api(tags = " 变压器-部件")
@RestController
@RequestMapping("/nyTransPart")
public class NyTransPartController extends BaseController {
    @Autowired
    private INyTransPartService nyTransPartService;
//    @Autowired
//    private RemoteBmCodeService remoteBmCodeService;

    /**
     * 查询变压器-部件列表
     */
    @ApiOperation(value="查询变压器-部件列表", notes="查询变压器-部件列表")
    @PreAuthorize(hasPermi = "energy:nyTransPart:list")
    @GetMapping("/list")
    public TableDataInfo list(NyTransPart nyTransPart) {
        startPage();
        List<NyTransPart> list = nyTransPartService.selectNyTransPartList(nyTransPart);
        return getDataTable(list);
    }

    /**
     * 导出变压器-部件列表
     */
    @ApiOperation(value="导出变压器-部件列表", notes="导出变压器-部件列表")
    @PreAuthorize(hasPermi = "energy:nyTransPart:export")
    @Log(title = "变压器-部件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NyTransPart nyTransPart) throws IOException {
        List<NyTransPart> list = nyTransPartService.selectNyTransPartList(nyTransPart);
        ExcelUtil<NyTransPart> util = new ExcelUtil<NyTransPart>(NyTransPart.class);
        util.exportExcel(response, list, "变压器-部件数据");
    }
    /**
     * 获取变压器-部件详细信息
     */
    @ApiOperation(value="获取变压器-部件详细信息", notes="获取变压器-部件详细信息")
    @PreAuthorize(hasPermi = "energy:nyTransPart:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(nyTransPartService.selectNyTransPartById(id));
    }

    /**
     * 新增变压器-部件
     */
    @ApiOperation(value="新增变压器-部件", notes="新增变压器-部件")
    @PreAuthorize(hasPermi = "energy:nyTransPart:add")
    @Log(title = "变压器-部件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NyTransPart nyTransPart) {
//        RemoteSysBmType remoteSysBmType= remoteBmCodeService.genCode(nyTransPart.getSysBmId(), SysBmCodeTypeValue.DEVICE);
//        String nowCode = remoteSysBmType.getNowCode();//生成的唯一编码
//        String[] deviceType = remoteSysBmType.getHierarchy().split("-");//层级
//        nyTransPart.setNum(nowCode);
//        nyTransPart.setDeviceType(deviceType[deviceType.length-1]);
        return toAjax(nyTransPartService.insertNyTransPart(nyTransPart));
    }

    /**
     * 修改变压器-部件
     */
    @ApiOperation(value="修改变压器-部件", notes="修改变压器-部件")
    @PreAuthorize(hasPermi = "energy:nyTransPart:edit")
    @Log(title = "变压器-部件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NyTransPart nyTransPart) {
        return toAjax(nyTransPartService.updateNyTransPart(nyTransPart));
    }

    /**
     * 删除变压器-部件
     */
    @ApiOperation(value="删除变压器-部件", notes="删除变压器-部件")
    @ApiImplicitParam(name = "ids", value = "变压器-部件ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "energy:nyTransPart:remove")
    @Log(title = "变压器-部件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(nyTransPartService.deleteNyTransPartByIds(ids));
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<NyTransPartExcelVO> util = new ExcelUtil<NyTransPartExcelVO>(NyTransPartExcelVO.class);
        util.importTemplateExcel(response, "仪表数据");
    }

    /**
     * 导入抄表记录列表,
     */
    @PreAuthorize(hasPermi = "energy:nyTransPart:edit")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, String meterType) throws Exception {
        ExcelUtil<NyTransPartExcelVO> util = new ExcelUtil<NyTransPartExcelVO>(NyTransPartExcelVO.class);
        List<NyTransPartExcelVO> buiRoomVOList = util.importExcel(file.getInputStream());
        //批量插入数据
        String message = nyTransPartService.importData(buiRoomVOList,meterType);
        return AjaxResult.success(message);
    }
}
